﻿using System.Collections;
using System.Collections.Generic;
using System.ComponentModel;
using UnityEngine;
using App.Common;
using QxFramework.Core;
using System;
public class BulletManager : LogicModuleBase, IBulletManager
{
    public AllBullet allBullet;
    public override void Init()
    {
        base.Init();
        if (!RegisterData(out allBullet))
        {
            InitAllData();
        }
    }
    public AllBullet GetAllBullet()
    {
        return allBullet;
    }

    private void InitAllData()
    {
        allBullet = new AllBullet();
    }
    public override void FixedUpdate()
    {
        base.FixedUpdate();
        UpdateAllMono();
    }
    /// <summary>
    /// 同步场上所有Bullet
    /// </summary>
    public void UpdateAllMono()
    {
        foreach (string key in allBullet._ObjList.Keys)
        {
            if (!allBullet._BulletList.ContainsKey(key))
            {
                RemoveObj(key);
                break;
            }
        }
        foreach (string key in allBullet._BulletList.Keys)
        {
            if (allBullet._ObjList.ContainsKey(key))
            {
                if (allBullet._ObjList[key] == null)
                {
                    allBullet._ObjList.Remove(key);
                    continue;
                }
            }
            else
            {
                AddObj(key);
            }
            MoveBullet(allBullet._BulletList[key]);
            allBullet._ObjList[key].transform.position = allBullet._BulletList[key].positionData.Position;
            allBullet._ObjList[key].transform.eulerAngles = new Vector3(0, 0, GetAngle(allBullet._BulletList[key].Direction));
        }
    }
    public float GetAngle(Vector3 Dir)
    {
        float Angle = 0;
        float Rate = Dir.y / Dir.x;
        if (Dir.x > 0)
        {
            Angle = (Mathf.Atan(Rate) / Mathf.PI) * 180;
        }
        else
        {
            Angle = 180 + (Mathf.Atan(Rate) / Mathf.PI) * 180;
        }
        return Angle;
    }
    public Bullet GetBullet(string name)
    {
        try
        {
            return allBullet._BulletList[name];
        }
        catch
        {
            return null;
        }
    }

    #region Bullet相关函数

    /// <summary>
    /// 添加一个对象
    /// </summary>
    /// <param name="mono"></param>
    public void AddMono(string Name, Bullet bullet)
    {
        string NameTemp = Name + "_0";
        int IDtemp = 0;
        while (allBullet._BulletList.ContainsKey(NameTemp))
        {
            IDtemp++;
            NameTemp = Name + "_" + IDtemp;
        }
        allBullet._BulletList.Add(NameTemp, bullet);
    }
    private void AddObj(string Name)
    {
        GameObject clone = ResourceManager.Instance.Instantiate("Prefabs/CreatedBullet/" + Name.Split('_')[0], GameObject.Find("SceneObjs").transform);
        clone.name = Name;
        clone.transform.position = allBullet._BulletList[Name].positionData.Position;
        allBullet._ObjList.Add(Name, clone);
    }
    /// <summary>
    /// 移除一个对象
    /// </summary>
    /// <param name="mono"></param>
    public void RemoveMono(string Name)
    {
        if (!allBullet._BulletList.ContainsKey(Name))
        {
            Debug.LogError("BulletList没有" + Name);
            return;
        }
        allBullet._BulletList.Remove(Name);
    }
    private void RemoveObj(string Name)
    {
        GameObject clone = allBullet._ObjList[Name];
        allBullet._ObjList.Remove(Name);
        ResourceManager.Instance.Destroy(clone);
    }

    /// <summary>
    /// 移动子弹
    /// </summary>
    /// <param name="mono"></param>
    public void MoveBullet(Bullet bullet)
    {
        bullet.positionData.Position += bullet.Direction.normalized * bullet.Speed;
    }
    #endregion

    public void ClearAll()
    {
        allBullet._BulletList = new Dictionary<string, Bullet>();
    }
}

public class AllBullet : GameDataBase {
    /// <summary>
    /// 场上的所有子弹data
    /// </summary>
    public Dictionary<string, Bullet> _BulletList = new Dictionary<string, Bullet>();
    /// <summary>
    /// 场上的所有子弹的Gameobj
    /// </summary>
    public Dictionary<string, GameObject> _ObjList = new Dictionary<string, GameObject>();
}

public class Bullet
{
    /// <summary>
    /// 调用子弹的技能ID
    /// </summary>
    public int TypeID = 0;

    /// <summary>
    /// 子弹类型
    /// </summary>
    public enum BulletType
    {
        projectile,
        ray,
    }

    public BulletType bulletType {
        get
        {
            if(TypeID == 0)
            {
                return BulletType.projectile;
            }
            if(TypeID == 1)
            {
                return BulletType.ray;
            }
            else
            {
                return BulletType.projectile;
            }
        }
    }

    /// <summary>
    /// 子弹将造成的伤害
    /// </summary>
    public int Damage = 0;

    /// <summary>
    /// 子弹速度
    /// </summary>
    public float Speed = 0;

    /// <summary>
    /// 子弹射程
    /// </summary>
    public float Range = 0;

    /// <summary>
    /// 子弹方向
    /// </summary>
    public Vector3 Direction = Vector3.zero;

    /// <summary>
    /// 子弹的碰撞状态
    /// </summary>
    public BulletCollisionNum collision = new BulletCollisionNum();

    /// <summary>
    /// 子弹的坐标数据
    /// </summary>
    public PositionData positionData = new PositionData();

    /// <summary>
    /// 子弹元素
    /// </summary>
    public MonoAttribute attribute;

    /// <summary>
    /// 一次添加buff层数
    /// </summary>
    public int AddBuffNum;

}
public class BulletCollisionNum
{
    /// <summary>
    /// 最大碰撞次数
    /// </summary>
    public int MaxNum = 0;

    /// <summary>
    /// 当前碰撞次数
    /// </summary>
    public int NowNum = 0;
}
public class PositionData
{
    /// <summary>
    /// 子弹坐标
    /// </summary>
    public Vector3 Position = Vector3.zero;
    /// <summary>
    /// 子弹初始坐标
    /// </summary>
    public Vector3 StartPosition = Vector3.zero;

}

